﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// ImageReplaceDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ImageReplaceDialog : MetroWindow
    {
        public ImageReplaceDialog(string oldImagePath, string shortImageFileCommentName)
        {
            InitializeComponent();
            this.shortImageFileCommentName = shortImageFileCommentName;
            Title = Globals.AppName + "- 替换图像";
            imgPath = oldImagePath;
            imgOld.Source = Globals.LoadBitmapImageFromFile(oldImagePath);

            // imgNew 的内容由调用方法提供
        }

        private void RbtnRenameNewImage_Checked(object sender, RoutedEventArgs e)
        {
            if (tbxNewFileShortName != null)
                tbxNewFileShortName.IsEnabled = true;
        }

        private void RbtnRenameNewImage_Unchecked(object sender, RoutedEventArgs e)
        {
            if (tbxNewFileShortName != null)
                tbxNewFileShortName.IsEnabled = false;
        }

        private string shortImageFileCommentName = "";

        public string ShortImageFileCommentName
        {
            get { return tbxNewFileShortName.Text.Trim(new char[] { ' ', '　', '\t', }); }
        }

        private string imgPath;

        public string ImgPath { get { return this.imgPath; } }

        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (rbtnOverrideOldImage.IsChecked == true)
            {
                this.DialogResult = true;
                this.Close();
            }
            else if (rbtnRenameNewImage.IsChecked == true)
            {
                try
                {
                    FileInfo oldInfo = new FileInfo(imgPath);

                    var dir = oldInfo.Directory.FullName;
                    if (dir.EndsWith("\\") == false)
                    {
                        dir += "\\";
                    }
                    var shortName = tbxNewFileShortName.Text.Trim(new char[] { ' ', '\t', '　', });
                    if (shortName.ToLower().EndsWith(".png") == false)
                    {
                        shortName += ".png";
                    }
                    shortName = ChinesePinYin.ToChinesePinYinText(shortName.Replace(" ", "-").Replace("　", "-").Replace("\t", "-"));
                    var fullName = dir + shortName;

                    if (File.Exists(fullName))
                    {
                        LMessageBox.ShowWarning($"您输入的【{tbxNewFileShortName.Text}】会转换为【{shortName}】。\r\n\r\n　　但是磁盘上也已经存在这个名字的文件了，请再换一个。", this);
                        return;
                    }

                    FileInfo newInfo = new FileInfo(fullName);

                    if (shortName.ToLower() == oldInfo.Name.ToLower())
                    {
                        LMessageBox.ShowWarning("您选择了“另取文件名”，可是并没有提供新的不重复的文件名。", this);
                        return;
                    }

                    imgPath = newInfo.FullName;
                    this.DialogResult = true;
                    this.Close();
                }
                catch (Exception ex)
                {
                    LMessageBox.ShowWarning(ex.Message, this);
                }
            }
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {
                FileInfo fi = new FileInfo(imgPath);
                tbxNewFileShortName.Text = shortImageFileCommentName;
                tbxNewFileShortName.Select(fi.Name.Length, 0);
                tbxNewFileShortName.Focus();
            }
            catch
            {
                tbxNewFileShortName.Text = "===文件名无效！！！===";
                tbxNewFileShortName.IsEnabled = false;
                rbtnOverrideOldImage.IsChecked = false;
                rbtnRenameNewImage.IsChecked = false;
                btnOK.IsEnabled = false;
            }
        }
    }
}
